﻿using BPMS.Business;
using BPMS.Entity;
using BPMS.IBusiness;
using DotNet.Utilities;
using DotNet.Utilities.DotNetFile;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace BPMS.MVC.Controllers
{
    public class ModuleController : BaseController
    {
        BPMS_SysMenuIBLL bpms_sysmenuibll = new BPMS_SysMenuBLL();
        BPMS_SysMenu bpms_sysmenu = new BPMS_SysMenu();
        BPMS_SysMenuButtonIBLL bpms_sysmenubuttonibll = new BPMS_SysMenuButtonBLL();

        #region 模块(菜单)列表
        /// <summary>
        /// 模块(菜单)列表
        /// </summary>
        /// <returns></returns>
        public ActionResult ModuleList()
        {
            return View();
        }

        /// <summary>
        /// 模块菜单列表
        /// </summary>
        public string GetMenuTreeTable()
        {
            StringBuilder TableTree_Menu = new StringBuilder();
            IList list = bpms_sysmenuibll.GetList();
            int eRowIndex = 0;
            foreach (BPMS_SysMenu entity in list)
            {
                if (entity.ParentId == "0")
                {
                    string trID = "node-" + eRowIndex.ToString();
                    TableTree_Menu.Append("<tr id='" + trID + "'>");
                    TableTree_Menu.Append("<td style='width: 230px;padding-left:20px;'><img src='/Themes/images/32/" + entity.Img + "' style='width:20px; height:20px;vertical-align: middle;' alt=''/><span style='padding-left:8px;'>" + entity.FullName + "</span></td>");
                    TableTree_Menu.Append("<td style='width: 100px;'>" + entity.Code + "</td>");
                    TableTree_Menu.Append("<td style='width: 130px;text-align:center;'>" + entity.Category + "</td>");
                    TableTree_Menu.Append("<td style='width: 60px;text-align:center;'>" + entity.Target + "</td>");
                    TableTree_Menu.Append("<td>" + entity.NavigateUrl + "</td>");
                    TableTree_Menu.Append("<td>" + entity.FormName + "</td>");
                    TableTree_Menu.Append("<td style='width: 60px;text-align:center;'>" + IsEnabled(entity.Enabled) + "</td>");
                    TableTree_Menu.Append("<td style='width: 60px;text-align:center;'>" + entity.SortCode + "</td>");
                    TableTree_Menu.Append("<td style='display:none'>" + entity.MenuId + "</td>");
                    TableTree_Menu.Append("</tr>");
                    //创建子节点
                    TableTree_Menu.Append(GetTableTreeNode(entity.MenuId, list, trID));
                    eRowIndex++;
                }
            }
            return TableTree_Menu.ToString();
        }
        /// <summary>
        /// 创建子节点
        /// </summary>
        /// <param name="parentID">父节点主键</param>
        /// <param name="list">菜单集合</param>
        /// <returns></returns>
        public string GetTableTreeNode(string parentID, IList list, string parentTRID)
        {
            StringBuilder sb_TreeNode = new StringBuilder();
            int i = 1;
            foreach (BPMS_SysMenu entity in list)
            {
                if (entity.ParentId == parentID)
                {
                    string trID = parentTRID + "-" + i.ToString();
                    sb_TreeNode.Append("<tr id='" + trID + "' class='child-of-" + parentTRID + "'>");
                    sb_TreeNode.Append("<td style='padding-left:20px;'><img src='/Themes/images/32/" + entity.Img + "' style='width:20px; height:20px;vertical-align: middle;' alt=''/><span style='padding-left:8px;'>" + entity.FullName + "</span></td>");
                    sb_TreeNode.Append("<td style='width: 100px;'>" + entity.Code + "</td>");
                    sb_TreeNode.Append("<td style='width: 130px;text-align:center;'>" + entity.Category + "</td>");
                    sb_TreeNode.Append("<td style='width: 60px;text-align:center;'>" + entity.Target + "</td>");
                    sb_TreeNode.Append("<td>" + entity.NavigateUrl + "</td>");
                    sb_TreeNode.Append("<td>" + entity.FormName + "</td>");
                    sb_TreeNode.Append("<td style='width: 60px;text-align:center;'>" + IsEnabled(entity.Enabled) + "</td>");
                    sb_TreeNode.Append("<td style='width: 60px;text-align:center;'>" + entity.SortCode + "</td>");
                    sb_TreeNode.Append("<td style='display:none'>" + entity.MenuId + "</td>");
                    sb_TreeNode.Append("</tr>");
                    //创建子节点
                    sb_TreeNode.Append(GetTableTreeNode(entity.MenuId, list, trID));
                    i++;
                }
            }
            return sb_TreeNode.ToString();
        }
        /// <summary>
        /// 是否有效
        /// </summary>
        /// <param name="Enabled">是否有效</param>
        /// <returns></returns>
        public string IsEnabled(int? Enabled)
        {
            if (Enabled == 1)
            {
                return "<img src='/Themes/Images/checkmark.gif'/>";
            }
            else
            {
                return "<img src='/Themes/Images/checknomark.gif'/>";
            }
        }

        /// <summary>
        /// 提交数据
        /// </summary>
        public void LoadAction()
        {
            string active = Request["action"];                                          //提交类型
            string key = Request["key"];                                                //主键
            BPMS_SysMenuIBLL bpms_sysmenuibll = new BPMS_SysMenuBLL();
            BPMS_SysMenu bpms_sysmenu = new BPMS_SysMenu();
            switch (active)
            {
                case "MenuTreeTable"://加载模块菜单列表
                    Response.Write(GetMenuTreeTable());
                    Response.End();
                    break;
                case "Delete"://删除模块菜单
                    bpms_sysmenu = bpms_sysmenuibll.GetEntity(key);
                    if (bpms_sysmenu.AllowDelete == 0)
                    {
                        Response.Write(string.Format(MessageHelper.MSG0009, bpms_sysmenu.FullName));
                        Response.End();
                    }
                    if (bpms_sysmenuibll.IsBelowMenu(key))
                    {
                        Response.Write(string.Format(MessageHelper.MSG0010, bpms_sysmenu.FullName));
                        Response.End();
                    }
                    else
                    {
                        Response.Write(bpms_sysmenuibll.Delete(key));
                        Response.End();
                    }
                    break;
                default:
                    break;
            }
        }
        #endregion

        #region 模块(菜单)表单
        /// <summary>
        /// 模块(菜单)表单
        /// </summary>
        /// <returns></returns>
        public ActionResult ModuleForm()
        {
            return View();
        }
        /// <summary>
        /// 确认事件
        /// </summary>
        /// <param name="bpms_sysmenu">实体类</param>
        /// <returns></returns>
        public ActionResult AcceptClick(BPMS_SysMenu bpms_sysmenu)
        {
            string key = Request["key"];                                                //主键
            bool IsOk = false;
            if (!string.IsNullOrEmpty(key))//判断是否编辑
            {
                bpms_sysmenu.MenuId = key;
                bpms_sysmenu.ModifyDate = DateTime.Now;
                bpms_sysmenu.ModifyUserId = SessionHelper.GetSessionUser().UserId;
                bpms_sysmenu.ModifyUserName = SessionHelper.GetSessionUser().UserName;
                IsOk = bpms_sysmenuibll.Update(bpms_sysmenu);
            }
            else
            {
                bpms_sysmenu.MenuId = CommonHelper.GetGuid;
                bpms_sysmenu.CreateUserId = SessionHelper.GetSessionUser().UserId;
                bpms_sysmenu.CreateUserName = SessionHelper.GetSessionUser().UserName;
                IsOk = bpms_sysmenuibll.Insert(bpms_sysmenu);
            }
            return Content(IsOk.ToString().ToLower());
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        /// <param name="key">主键</param>
        /// <returns></returns>
        public ActionResult InitControl(string key)
        {
            if (!string.IsNullOrEmpty(key))
            {
                bpms_sysmenu = bpms_sysmenuibll.GetEntity(key);
                string strJson = JsonHelper.ObjectToJson(bpms_sysmenu);
                //获取上级菜单信息
                bpms_sysmenu = bpms_sysmenuibll.GetEntity(bpms_sysmenu.ParentId);
                strJson = strJson.Insert(1, "\"ParentName\":\"" + bpms_sysmenu.FullName + "\",");
                return Content(strJson);
            }
            return null;
        }
        /// <summary>
        /// 模块菜单树
        /// </summary>
        public ActionResult GetMenuTree()
        {
            IList list = bpms_sysmenuibll.GetList();
            StringBuilder strHtml = new StringBuilder();
            List<BPMS_SysMenu> itemNode = IListHelper.IListToList<BPMS_SysMenu>(list).FindAll(t => t.ParentId == "0");
            strHtml.Append("<li>");
            strHtml.Append("<div id='0'>父节点</div>");
            strHtml.Append("</li>");
            foreach (BPMS_SysMenu entity in itemNode)
            {
                strHtml.Append("<li>");
                strHtml.Append("<div id='" + entity.MenuId + "'>" + entity.FullName + "</div>");
                //创建子节点
                strHtml.Append(GetTreeNode(entity.MenuId, list));
                strHtml.Append("</li>");
            }
            return Content(strHtml.ToString());
        }
        /// <summary>
        /// 创建子节点
        /// </summary>
        /// <param name="parentID">父节点主键</param>
        /// <param name="list">菜单集合</param>
        /// <returns></returns>
        public string GetTreeNode(string ParentId, IList list)
        {
            StringBuilder sb_TreeNode = new StringBuilder();
            List<BPMS_SysMenu> itemNode = IListHelper.IListToList<BPMS_SysMenu>(list).FindAll(t => t.ParentId == ParentId);
            if (itemNode.Count > 0)
            {
                sb_TreeNode.Append("<ul>");
                foreach (BPMS_SysMenu entity in itemNode)
                {
                    sb_TreeNode.Append("<li>");
                    sb_TreeNode.Append("<div id='" + entity.MenuId + "'>" + entity.FullName + "</div>");
                    //创建子节点
                    sb_TreeNode.Append(GetTreeNode(entity.MenuId, list));
                    sb_TreeNode.Append("</li>");
                }
                sb_TreeNode.Append("</ul>");
            }
            return sb_TreeNode.ToString();
        }
        #endregion

        #region 模块按钮
        StringBuilder htmlButtontoolbar = new StringBuilder();
        StringBuilder htmlButtonApplication = new StringBuilder();
        /// <summary>
        /// 模块按钮
        /// </summary>
        /// <returns></returns>
        public ActionResult ModuleButton()
        {
            string _Menu_Id = Request["Menu_Id"];//模块主键
            string _MenuName = Server.UrlDecode(Request["Menu_Name"]);//模块名称
            string _MenuImg = Request["Menu_Img"];//模块图标
            ViewData["_Menu_Id"] = _Menu_Id;
            ViewData["_MenuName"] = _MenuName;
            ViewData["_MenuImg"] = _MenuImg;
            InitButton(_Menu_Id);
            ViewData["htmlButtontoolbar"] = htmlButtontoolbar;
            ViewData["htmlButtonApplication"] = htmlButtonApplication;
            return View();
        }
        /// <summary>
        /// 加载模块按钮
        /// </summary>
        /// <param name="Menu_Id"></param>
        public void InitButton(string Menu_Id)
        {
            DataTable dt = bpms_sysmenubuttonibll.GetListWhere(Menu_Id);
            if (DataTableHelper.IsExistRows(dt))
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string checkbuttonNo = "checkbuttonNo";
                    string triangleNo = "triangleNo";
                    if (CommonHelper.GetDateTime(dt.Rows[i]["IsExist"]) < DateTime.Now)
                    {
                        checkbuttonNo = "checkbuttonOk";
                        triangleNo = "triangleOk";
                    }
                    if (dt.Rows[i]["Category"].ToString() == "工具栏")
                    {
                        htmlButtontoolbar.Append("<div title=\"" + dt.Rows[i]["Description"] + "\" class=\"" + checkbuttonNo + " panelcheck\">");
                        htmlButtontoolbar.Append("<div id=\"" + dt.Rows[i]["ButtonId"] + "\" class=\"checktext\">");
                        htmlButtontoolbar.Append("<img src=\"../../Themes/Images/16/" + dt.Rows[i]["Img"] + "\" />" + dt.Rows[i]["FullName"] + "");
                        htmlButtontoolbar.Append("</div>");
                        htmlButtontoolbar.Append("<div class=\"" + triangleNo + "\"></div>");
                        htmlButtontoolbar.Append("</div>");

                    }
                    else if (dt.Rows[i]["Category"].ToString() == "右击菜单栏")
                    {
                        htmlButtonApplication.Append("<div title=\"" + dt.Rows[i]["Description"] + "\" class=\"" + checkbuttonNo + " panelcheck\">");
                        htmlButtonApplication.Append("<div id=\"" + dt.Rows[i]["ButtonId"] + "\" class=\"checktext\">");
                        htmlButtonApplication.Append("<img src=\"../../Themes/Images/16/" + dt.Rows[i]["Img"] + "\" />" + dt.Rows[i]["FullName"] + "");
                        htmlButtonApplication.Append("</div>");
                        htmlButtonApplication.Append("<div class=\"" + triangleNo + "\"></div>");
                        htmlButtonApplication.Append("</div>");
                    }
                }
            }
        }
        /// <summary>
        /// 确认事件
        /// </summary>
        /// <returns></returns>
        public ActionResult Accept_ModuleButton()
        {
            string _Menu_Id = Request["Menu_Id"];
            string _ItemButtonId = Server.UrlDecode(Request["item"]);
            string[] array = new string[0];
            if (!string.IsNullOrEmpty(_ItemButtonId))
            {
                array = _ItemButtonId.Split('≌');
            }
            bool IsOk = false;
            string CreateUserId = SessionHelper.GetSessionUser().UserId;
            string CreateUserName = SessionHelper.GetSessionUser().UserName;
            IsOk = bpms_sysmenubuttonibll.BatchAddMenuButton(array, _Menu_Id, CreateUserId, CreateUserName);
            return Content(IsOk.ToString().ToLower());
        }
        #endregion

        #region 模块(菜单)图标
        /// <summary>
        /// 模块(菜单)图标
        /// </summary>
        /// <returns></returns>
        public ActionResult ModuleIcon()
        {
            string _Size = Request["Size"];//图标类型，32,16
            ViewData["strImg"] = GetImg(_Size); ;
            return View();
        }
        /// <summary>
        /// 加载图标
        /// </summary>
        /// <param name="_Size"></param>
        /// <returns></returns>
        public string GetImg(string _Size)
        {
            StringBuilder strImg = new StringBuilder();
            int PageSize = 0;
            int PageIndex = 1;
            DirectoryInfo dir;
            if (_Size == "32")
            {
                PageSize = 100;
                dir = new DirectoryInfo(Server.MapPath("/Themes/Images/32/"));
            }
            else
            {
                PageSize = 200;
                dir = new DirectoryInfo(Server.MapPath("/Themes/Images/16/"));
            }
            int rowCount = 0;
            int rowbegin = (PageIndex - 1) * PageSize;
            int rowend = PageIndex * PageSize;
            FileInfo[] files = dir.GetFiles();
            MyDateSorter.QuickSort(files, 0, files.Length - 1);//按时间排序
            foreach (FileInfo fsi in files)
            {
                if (rowCount >= rowbegin && rowCount < rowend)
                {
                    strImg.Append("<div class=\"divicons\" title='" + fsi.Name + "'>");
                    strImg.Append("<img src=\"/Themes/Images/" + _Size + "/" + fsi.Name + "\" />");
                    strImg.Append("</div>");
                }
                rowCount++;
            }
            //this.PageControl.RecordCount = Convert.ToInt32(rowCount);
            return strImg.ToString();
        }
        #endregion
    }
}
